package org.wang.chuangen.session.db;

import java.nio.charset.Charset;

import org.wang.chuangen.session.util.JavaByteSerialization;
import org.wang.chuangen.session.util.JsonTextSerialization;
import org.wang.chuangen.session.util.Serialization;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

/**
 * Redis数据库链接管理类
 * @author 王传根
 * @date 2016-5-12下午1:29:06
 */
public class RedisConn {
	/**
	 * 最小timeout时间
	 */
	public static final int MIN_SESSION_TIMEOUT = 600;
	/**
	 * 使用java默认的序列化方式
	 */
	public static final String JAVA_BYTE = "byte";
	/**
	 * 使用json方式序列化对象
	 */
	public static final String JSON_TEXT = "json";
	
	/**
	 * redis服务器地址
	 */
	protected String host = "localhost";
	
	/**
	 * redis服务端口
	 */
	protected int port = 6379;
	
	/**
	 * 数据库索引
	 */
	protected int database = 0;
	
	/**
	 * redis密码
	 */
	protected String password = null;
	
	/**
	 * session超时时间
	 */
	protected int sessionTimeout = 600;
	
	/**
	 * 超时时间
	 */
	protected int timeout = Protocol.DEFAULT_TIMEOUT;
	
	/**
	 * 序列化方式
	 */
	protected String serialize=JAVA_BYTE;
	
	/**
	 * 编码方式
	 */
	protected Charset charset=Charset.defaultCharset();
	
	/**
	 * redis数据库链接池
	 */
	protected JedisPool connPool;
	
	/**
	 * 序列化工具类
	 */
	protected Serialization serialization;
	
	/**
	 * 单例实例
	 */
	private static final RedisConn REDIS_CONN=new RedisConn();
	
	private RedisConn(){}
	
	public static RedisConn getInstance(){
		return REDIS_CONN;
	}
	
	public void setHost(String host) {
		this.host = host;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public int getSessionTimeout() {
		return sessionTimeout;
	}

	public void setSessionTimeout(int sessionTimeout) {
		if(sessionTimeout > MIN_SESSION_TIMEOUT){
			this.sessionTimeout = sessionTimeout;
		}
	}
	
	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		if(timeout > Protocol.DEFAULT_TIMEOUT){
			this.timeout = timeout;
		}
	}

	public int getDatabase() {
		return database;
	}

	public void setDatabase(int database) {
		this.database = database;
	}

	public void setSerialize(String serialize) {
		this.serialize = serialize;
	}

	public Charset getCharset() {
		return charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	public JedisPool getConnPool(){
		return connPool;
	}
	
	public Serialization getSerialization() {
		return serialization;
	}

	public void initialization(){
		connPool=new JedisPool(new JedisPoolConfig(), host, port, timeout, password);
		if(RedisConn.JSON_TEXT.equalsIgnoreCase(serialize)){
			serialization=new JsonTextSerialization(charset);
		}else{
			serialization=new JavaByteSerialization();
		}
	}
	
	public void destroy(){
		if(connPool!=null)connPool.destroy();
	}
}
